//
//  TongJIFenXiVC.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TongJIFenXiVC.h"
#import "YYCollectionViewCell.h"
#import "HeaderCollectionViewCell.h"
#import "ChartCollectionViewCell.h"
#import "CollectionReusableView11.h"
#import "TongJIDetailVC.h"
#import "TongJiModel.h"

@interface TongJIFenXiVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong)NSDictionary *dataDictionNaray;

@property (nonatomic, strong)NSMutableArray *biliArray;

@property (nonatomic, strong)NSMutableArray *studentArray;

@end

@implementation TongJIFenXiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"统计分析";
    
    [self loadData];
    
    [self setcollectionView];
    
    
    
}

-(void)loadData{
    _dataDictionNaray = [NSDictionary dictionary];
    _biliArray = [NSMutableArray array];
    _studentArray = [NSMutableArray array];
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    NSString *url = [NSString string];
    
    if (_isComeFormTi == YES) {
        url = @"g=Client&m=Task&a=paperReport";
        
        parameter.body = @{@"task_id":_task_id,
                           @"paper_id":_course_id
                           };
    }else{
        url = @"g=Client&m=Task&a=courseReport";
        parameter.body = @{@"task_id":_task_id,
                           @"course_id":_course_id
                           };
    }
    

    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,url] parameter:parameter sucess:^(id responseObject) {

        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            
             _dataDictionNaray = responseObject[@"data"];
            for (NSDictionary *dicc in _dataDictionNaray[@"question_list_percent"]) {
                TongJiModel *model = [TongJiModel new];
                [model setValuesForKeysWithDictionary:dicc];
                [_biliArray addObject:model];
            }
            
            
            for (NSDictionary *dicc in _dataDictionNaray[@"student_list"]) {
                TongJiModel *model = [TongJiModel new];
                [model setValuesForKeysWithDictionary:dicc];
                [_studentArray addObject:model];
            }
            
        
            [self.collectionView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)setcollectionView{
    //设置布局对象
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //设置item大小
    _flowLayout.itemSize = CGSizeMake(50 * KscreenWidth, 100 * KscreenHeight);
    
    //设置最小列间距
    _flowLayout.minimumInteritemSpacing = 40 * KscreenWidth;
    
    //设置最小行间距
    _flowLayout.minimumLineSpacing = 10 * KscreenHeight;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, Screenheight) collectionViewLayout:_flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //设置代理
    _collectionView.delegate = self;
    
    //设置数据源
    _collectionView.dataSource = self;
    
    //注册
    [_collectionView registerClass:[YYCollectionViewCell class] forCellWithReuseIdentifier:@"YYCollectionViewCell"];
    
     [_collectionView registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:@"HeaderCollectionViewCell"];
    
    [_collectionView registerClass:[ChartCollectionViewCell class] forCellWithReuseIdentifier:@"ChartCollectionViewCell"];    
    
#pragma mark -- 注册头部视图
    [_collectionView registerClass:[CollectionReusableView11 class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView11"];
    
    //添加到视图
    [self.view addSubview:_collectionView];
}


#pragma mark 必须实现的方法
//每分区有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
         return _biliArray.count;
    }else{
        
        return _studentArray.count;
    }
   
}

//每个item上面现实的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        //从重用池里面获取cell
        HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderCollectionViewCell" forIndexPath:indexPath];
        cell.numStr = [[_dataDictionNaray valueForKey:@"complete_percent"] longValue];
        
        return cell;
        
    }else if (indexPath.section == 1){
        ChartCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChartCollectionViewCell" forIndexPath:indexPath];
        
        TongJiModel *model1 = _biliArray[indexPath.row];
        
        NSLog(@"%ld",model1.right_percent);
        
        cell.model = model1;
        
        cell.nameLable.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
        
        return cell;
        
    }else{
        //从重用池里面获取cell
        YYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YYCollectionViewCell" forIndexPath:indexPath];
        
//        cell.nameLable.text = @"张三";

        TongJiModel *model1 = _studentArray[indexPath.row];
        [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:model1.small_img] placeholderImage:[UIImage imageNamed:@"0009"]];
        cell.nameLable.text = model1.name;
        
        return cell;

    }

}

//设置分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

         return 3;
   
}


//点击item触发的事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        TongJIDetailVC *tongjiDetailVC = [[TongJIDetailVC alloc]init];
        tongjiDetailVC.task_id = _task_id;
        tongjiDetailVC.course_id = _course_id;
        if (_isComeFormTi == YES) {
            tongjiDetailVC.iscomeFromTi = YES;
        }else{
            tongjiDetailVC.iscomeFromTi = NO;
        }
        
         TongJiModel *model1 = _studentArray[indexPath.row];
        
        tongjiDetailVC.student_id = model1.id;
        tongjiDetailVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:tongjiDetailVC animated:NO completion:^{
          
        }];
    }
}

//返回分区内边距的上左下右的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 150 * KscreenHeight, 0);
    }else if (section == 1){
        
        return UIEdgeInsetsMake(30 * KscreenWidth, 30 *KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight);
    }else{
        
      return UIEdgeInsetsMake(30 * KscreenHeight, 30 *KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight);
    }
    
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        CGSize size = CGSizeMake(SCREEN_WIDTH, 80 * KscreenHeight);
        return size;
    }else{
        
        CGSize size = CGSizeMake(SCREEN_WIDTH, 90 * KscreenHeight);
        return size;
    }

    
    
}

#pragma mark - 头部视图内容
//设置头尾部内容

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView =nil;
    
    if (kind ==UICollectionElementKindSectionHeader) {
        
        CollectionReusableView11 *headV = (CollectionReusableView11 *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView11" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headV.nameLable.text = @"任务完成比率";
            headV.detailLable.text = [_dataDictionNaray valueForKey:@"top_complete_word"];
        }else if (indexPath.section == 1){
            headV.nameLable.text = @"试卷统计";
            
            if (_biliArray.count == 0) {
                headV.detailLable.text = @"暂无数据";
            }else{
                headV.detailLable.text = [NSString stringWithFormat:@"单选题(%lu道)",(unsigned long)_biliArray.count];
            }
            
        }else{
            headV.nameLable.text = @"详细统计";
            
            if (_studentArray.count == 0) {
                headV.detailLable.text = @"暂无数据";
            }else{
                headV.detailLable.text = [NSString stringWithFormat:@"共%lu个学生",(unsigned long)_studentArray.count];
            }
            
        }
        
        if (indexPath.section == 0) {
            headV.vie.hidden = YES;
        }else{
            headV.vie.hidden = NO;
        }
        
        reusableView = headV;
        
    }
    
    return reusableView;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
